// pages/productdetails/productdetails.js
import { productdetails, addshop, shoucang, dqgoods,collectcheck, delcollect } from '../../utils/http'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    swiper: [],
    swipe: [],
    list: [],
    show: false,
    goodsId: '',
    number: 1,
    //详情图
    content: null,
    length:null,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.list)
    wx.setNavigationBarTitle({
      title: '商品详情'
    })
    productdetails(options.id).then(res => {
   
      this.setData({ content: res.data.content, swiper: res.data.pics, list: res.data.basicInfo, swipe: res.data.pics[0], goodsId: res.data.pics[0].goodsId })
    })

      this.getlist()
  },
      // 请求购物车数据
      getlist(){
        let token = wx.getStorageSync('token')
        dqgoods(token).then(res => {
            console.log(res.data.items.length)
            this.setData({length:res.data.items.length})
        })
    },
  // 底部弹出
  onClickButton() {
    this.setData({ show: true });
  },
  onClose() {
    this.setData({ show: false });
  },
  //   加减
  onChange(event) {
    console.log(event.detail);
    this.setData({ number: event.detail })
  },
  //加入购物车
  addshop() {
    let token = wx.getStorageSync('token')
    console.log(token)
    let goodsId = this.data.goodsId
    // console.log(goodsId)
    let number = this.data.number
    addshop(token, goodsId, number).then(res => {
  
      let data = JSON.stringify(res.data)
      wx.reLaunch({
        url: '/pages/goods/goods?list=' + data
      })
    })
  },
  //收藏
  shoucang(e) {
    let id = e.currentTarget.dataset.id
    let token = wx.getStorageSync('token')
    console.log(id, token)
    collectcheck(id, token).then(res => {
      console.log(res)
      if (res.code == '-1') {
        shoucang(id, token).then(res => {
          console.log(res)
          wx.showToast({
            title: '收藏成功',
            icon: 'success'
          })
        })
      } else {
        delcollect(id, token).then(res => {
          console.log(res)
          wx.showToast({
            title: '取消收藏',
            icon: 'success'
          })
        })
      }
    })
  },
})